﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ReadManga.GUI
{
    public partial class PageReader : PhoneApplicationPage
    {
        WebClient client = new WebClient();
        string Url;
        private bool UnLockRotation = false;

        public PageReader()
        {
            InitializeComponent();
            
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            
            //NavigationContext.QueryString.TryGetValue("Url", out Url);

            LoadMangaURL(Global.selectedChap.Url);
        }
        void LoadMangaURL(string Url)
        {
            if (Url != "")
            {
                WaitProcess.Visibility = Visibility.Visible;
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                client.DownloadStringAsync(new Uri(Url), "blogtruyen");
            }
        }
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (e.UserState as string == "blogtruyen")
            {
            }
        }
        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null && !e.Cancelled)
            {
                LoadBrowser(Utils.GetStringBetweenText(e.Result, "<div class=\"noidungchap\">", "</div>"));
            }
        }
        void LoadBrowser(string content)
        {
            string htmlText="<html>";
            string CheckContent = content;
            string line = "";
            line = Utils.GetStringBetweenText(CheckContent, "<img src=\"", "\"");
            while (line != "")
            {

                htmlText+="<br><img src='"+line+"' width='100%'><br>";

                CheckContent = Utils.GetStringBetweenText(CheckContent, line, "");

                line = Utils.GetStringBetweenText(CheckContent, "<img src=\"", "\"");
            }
            webBrowser1.NavigateToString(htmlText);
        }


        private void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            
        }

        private void webBrowser1_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            WaitProcess.Visibility = Visibility.Collapsed;
        }

        private void BtnFirst_Click(object sender, EventArgs e)
        {
            Global.selectedChap = Global.selectedListChap[Global.selectedListChap.Count - 1];
            {
                LoadMangaURL(Global.selectedChap.Url);
            }
        }

        private void BtnLast_Click(object sender, EventArgs e)
        {
            Global.selectedChap = Global.selectedListChap[0];
            LoadMangaURL(Global.selectedChap.Url);
        }

        private void BtnPreview_Click(object sender, EventArgs e)
        {
            if (Global.selectedChap.index < Global.selectedListChap.Count-1)
            {
                Global.selectedChap = Global.selectedListChap[Global.selectedChap.index + 1];

                LoadMangaURL(Global.selectedChap.Url);
            }
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            if (Global.selectedChap.index > 0)
            {
                Global.selectedChap = Global.selectedListChap[Global.selectedChap.index - 1];

                LoadMangaURL(Global.selectedChap.Url);
            }
        }

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            string navigate = "";
            navigate += "Title=" +  Global.selectedItem.Title;
            navigate += "&UrlImage=" + HttpUtility.UrlEncode( Global.selectedItem.UrlImage);
            navigate += "&Url=" + HttpUtility.UrlEncode( Global.selectedItem.Url);
            navigate += "&Description=" +  Global.selectedItem.Description;
            NavigationService.Navigate(new Uri("/GUI/ViewDetails.xaml?" + navigate, UriKind.Relative));
        }

        private void PhoneApplicationPage_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
               
        }

        private void webBrowser1_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            
        }

        private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e)
        {
            if (UnLockRotation)
            {
                SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
            }
            else
            {
                switch (Orientation)
                {
                    case PageOrientation.None:
                    case PageOrientation.Portrait:
                    case PageOrientation.PortraitUp:
                    case PageOrientation.PortraitDown:
                        SupportedOrientations = SupportedPageOrientation.Portrait;
                        break;
                    case PageOrientation.Landscape:
                    case PageOrientation.LandscapeLeft:
                    case PageOrientation.LandscapeRight:
                        SupportedOrientations = SupportedPageOrientation.Landscape;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
            UnLockRotation = !UnLockRotation;
            
        }
    }
}